<template>
  <div class="login-bgc">

    <!-- 卡片登录区 -->
    <el-card class="login-k">
      <!-- 头像区域 -->
      <div class="avater_box">
        <img src="../assets/chestnut.jpg"
             alt="">
      </div>
      <!-- 表单区 -->
      <el-form :model="loginForm"
               :rules="loginFormRules"
               ref="loginFormRef"
               label-width="10px"
               class="formPosition">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    placeholder="请输入用户名"
                    prefix-icon="el-icon-s-custom"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    placeholder="请输入密码"
                    show-password
                    prefix-icon="el-icon-key"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button class="btn" @click="resetLoginForm()">重置</el-button>
        </el-form-item>
      </el-form>

    </el-card>
       <login-footer></login-footer>
  </div>
</template>
<script>
import loginFooter from './loginFooter.vue'
export default {
  components: {
    loginFooter

  },
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        // 这是来验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮 重置表单
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
      //   console.log(this)
    },
    // 点击确认 提交表单信息并重置表单
    login () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('api/login', this.loginForm)
        // console.log(res.status !== 0)
        if (res.status !== 0) {
          return this.$message.error('登录失败！')
        }
        this.$message.success('登录成功！')
        // console.log(res)
        window.sessionStorage.setItem('token', res.token)
        // 编程式导航跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>

.login-k {
  width: 600px;
  height: 400px;
  border-radius: 30px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avater_box {
  height: 130px;
  width: 130px;
  border-radius: 50%;
  border: 1px solid #eee;
  padding: 10px;
  background-color: #eee;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.formPosition {
  position: absolute;
  top: 175px;
  left: 175px;
}
.btn {
  position: absolute;
  right: 0;
}

</style>
